package com.ncloud.monitor.zabbix4j.template;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.monitor.zabbix4j.ZabbixApiException;
import com.ncloud.monitor.zabbix4j.ZabbixApiMethod;

/**
 * @author Suguru Yajima
 */
public class Template extends ZabbixApiMethod {
    public Template(String apiUrl, String auth) {
        super(apiUrl, auth);
    }

    public TemplateCreateResponse create(TemplateCreateRequest request) throws ZabbixApiException {
        TemplateCreateResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, TemplateCreateResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public TemplateDeleteResponse delete(TemplateDeleteRequest request) throws ZabbixApiException {
        TemplateDeleteResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, TemplateDeleteResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public TemplateUpdateResponse update(TemplateUpdateRequest request) throws ZabbixApiException {
        TemplateUpdateResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, TemplateUpdateResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public TemplateGetResponse get(TemplateGetRequest request) throws ZabbixApiException {
        TemplateGetResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, TemplateGetResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }
}
